#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#include "../common.h"
extern "C" {
#include "../yy.h"
}
#include "../sizer.h"
#include "../pcoder.h"
#include "../display.h"
#include "pic16e.h"
#include "pic16e_asm.h"
#include "pic16e_inst.h"

void PIC16E :: incValue(Item *ip, int value)
{
	switch (ip->type) {
		case IMMD_ITEM:	
		case ID_ITEM: {

			/**
			 *         lui     a5,%hi(a)
						lw      a5,%lo(a)(a5)
						addi    a4,a5,1
						lui     a5,%hi(a)
						sw      a4,%lo(a)(a5)
			 */
			if (ip->attr->isStatic == -1){
				char addBuf[64];
				if (canUserImmd(value)){ sprintf(addBuf,"\taddi\ta5,a6,%d\n", value);
				}else { sprintf(addBuf,"\tli  \ta5,%d\n"
							"\tadd \ta5, a6, a5\n" , value); }
				char buf[256]; sprintf(buf,
				"\tlui \ta6,%%hi(%s)\n"
				"\tlw  \ta6, %%lo(%s),(a6)\n"
				"%s"
				"\tlui \ta6, %%hi(%s)\n"
				"\tsw  \ta5,%%lo(%s)(a6)\n", 
				ip->vname,ip->vname, addBuf ,ip->vname, ip->vname); 
				ASM_OUTP(buf);
				break;
			}
			if (curFunc != NULL)
			{
				if (ip->fname != curFunc->name){ errPrint("function not matched error"); }
				Dnode* dp = serachVarable(ip->vname);	
				ASM_CODE(_LW, _R_A5, dp->memStr);
				char addBuf[128];
				if (canUserImmd(value)){ sprintf(addBuf,"\taddi\ta5,a5,%d\n", value); }
				else { sprintf(addBuf,"\tli  \ta61,%d\n"
							"\tadd \ta5, a6, a5\n" , value); 
				}
				ASM_OUTP(addBuf);
				ASM_CODE(_SW,_R_A5, dp->memStr);
			}
		}
		break;
		case TEMP_ITEM:{
			if (curFunc != NULL){
				char addBuf[128];
				if (canUserImmd(value)){ sprintf(addBuf,"\taddi\tt%d,t%d,%d\n", ip->val.i, ip->val.i ,value); }
				else { sprintf(addBuf,"\tli  \ta5,%d\n"
							"\tadd \tt%d, t%d, a5\n" ,value,ip->val.i, ip->val.i); 
				}
				ASM_OUTP(addBuf);
			}
		}break;
		default:
			errPrint("parse inc isa error!");
	}
}
